﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AddIn.EA.Architecture.ComponentLogic.ElementModel
{
    public class Package : BaseModel
    {
        public List<Package> Packages { get; set; }
        public List<Element> Elements { get; set; }

        public Package()
        {
            Packages = new List<Package>();
            Elements = new List<Element>();
        }
        public Package(String packageName)
            : this()
        {
            this.Name = packageName;
        }

        public Package FindPackage(string name)
        {
            return FindPackage(name, false);
        }
        public Package FindPackage(string name, bool recursive)
        {
            Package result = null;
            if (Packages != null && Packages.Count > 0)
            {
                if (recursive)
                    result = SearchPackage(name, Packages);
                else
                    result = Packages.Find(it => it.Name.CompareTo(name) == 0);
            }
            return result;
        }
        private Package SearchPackage(string name, List<Package> Packages)
        {
            Package result = null;
            if (Packages != null && Packages.Count > 0)
            {
                foreach (Package item in Packages)
                {
                    if (item.Name.CompareTo(name) == 0)
                    {
                        result = item;
                        break;
                    }

                    if (item.Packages != null && item.Packages.Count > 0)
                        result = SearchPackage(name, item.Packages);
                }
            }
            return result;
        }
    }
}
